<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="款名/款号/套装编码" class="myform-item">
          <el-input
            v-model="form.title"
            placeholder="请输入款名/款号/套装编码"
            @keyup.enter.native="onSearch"
            size="normal"
          ></el-input>
        </el-form-item>
        <el-form-item label="年份" class="myform-item">
          <el-date-picker
            v-model="form.new_year"
            type="year"
            placeholder="选择年"
            value-format="yyyy"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="月份" class="myform-item">
          <el-date-picker v-model="form.new_month" type="month" format="M" value-format="M" placeholder="选择月">
          </el-date-picker>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column
        label="套装编码"
        align="center"
        prop="package_number"
        width="200"
      ></el-table-column>
      <el-table-column label="人台图" width="280" align="center" prop="rttimg">
        <template slot-scope="{ row }">
          <div class="filemy">
            <div
              class="filemy-img"
              v-for="(item, index) in row.rttimg"
              :key="item"
              @mouseover="url = item"
              @mouseleave="url = ''"
            >
              <el-image
                :src="item"
                fit="cover"
                class="filemy-img-image"
              ></el-image>
              <div class="filemy-img-popup" v-if="item == url">
                <i class="el-icon-zoom-in" @click="onImagePreview(item)"></i>
                <i
                  class="el-icon-delete"
                  @click="onImageDelete(row, index, 1)"
                ></i>
              </div>
            </div>

            <i
              class="el-icon-plus avatar-uploader-iconmy pointer"
              @click="onUpload(row, 1)"
              v-if="row.house_status == 0"
            ></i>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="相关款信息" align="center" prop="list">
        <template slot-scope="{ row }">
          <el-table :data="row.list" style="width: 100%" border>
            <el-table-column
              label="款号"
              align="center"
              prop="type_no"
            ></el-table-column>
            <el-table-column
              label="款名"
              align="center"
              prop="type_name"
            ></el-table-column>
            <el-table-column
              label="产品图"
              width="340"
              align="center"
              prop="protimg"
            >
              <template slot-scope="{ row }">
                <div class="filemy">
                  <div
                    class="filemy-img"
                    v-for="(item, index) in row.protimg"
                    :key="item"
                    @mouseover="url = item"
                    @mouseleave="url = ''"
                  >
                    <el-image
                      :src="item"
                      fit="cover"
                      class="filemy-img-image"
                    ></el-image>
                    <div class="filemy-img-popup" v-if="item == url">
                      <i
                        class="el-icon-zoom-in"
                        @click="onImagePreview(item)"
                      ></i>
                      <i
                        class="el-icon-delete"
                        @click="onImageDelete(row, index, 2)"
                      ></i>
                    </div>
                  </div>

                  <i
                    class="el-icon-plus avatar-uploader-iconmy pointer"
                    @click="onUpload(row, 2)"
                    v-if="row.house_status == 0"
                  ></i>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="名称"
              align="center"
              prop="abbreviation"
            ></el-table-column>
            <el-table-column
              label="一级分类"
              align="center"
              prop="category"
            ></el-table-column>
            <el-table-column
              label="二级分类"
              align="center"
              prop="design_classification"
            ></el-table-column>
            <el-table-column
              label="上新年份"
              align="center"
              prop="new_year"
            ></el-table-column>
            <el-table-column
              label="上新月份"
              align="center"
              prop="new_month"
            ></el-table-column>
            <el-table-column label="备注" align="center" prop="house_text">
              <template slot-scope="{ row }">
                <div>
                  {{ row.house_text }}
                </div>
                <el-button type="text" @click="onHouseText(row)" v-if="row.house_status == 0">
                  <i class="el-icon-edit-outline" style="font-size: 20px"></i>
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column
        label="确认状态"
        align="center"
        prop="house_status"
        width="120"
      >
        <template slot-scope="{ row }">
          <div :style="{ color: row.house_status == 1 ? '#67C23A' : '' }">
            {{ row.house_status == 1 ? "已确认" : "未确认" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            size="default"
            @click="onAffirm(row, 1)"
            v-if="row.house_status == 0"
            >确认</el-button
          >
          <el-button
            type="text"
            style="color: red"
            size="default"
            @click="onAffirm(row, 0)"
            v-if="row.house_status == 1"
            >解锁</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminOperateGetProducts_house"
    />

    <el-upload
      class="avatar-uploader"
      action="/"
      ref="uploadmy"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
    >
    </el-upload>

    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import {
  postAdminOperateGetProducts_house,
  uploadHomeApiupload,
  postAdminOperateSetHumanplatform,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      dialogImageUrl: "",
      url: "",
      form: {
        title: "",
        new_year: "",
        new_month: "",
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
      row: {},
      group_by: "",
    };
  },
  activated() {
    let form = this.form;
    this.postAdminOperateGetProducts_house(form);
  },

  methods: {
    // 操作
    onAffirm(row, house_status) {
      let that = this;
      let obj = {
        package_number: row.package_number,
        house_status: house_status,
      };
      this.$confirm(`${house_status == 1 ? "确认" : "解锁"}此条信息?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await postAdminOperateSetHumanplatform(obj).then((res) => {
            this.$message({
              type: "success",
              message: "成功",
            });
          });
          let form = that.form;
          this.postAdminOperateGetProducts_house(form);
        })
        .catch(() => {
          // this.$message({
          //   type: "info",
          //   message: "已取消删除",
          // });
        });
    },
    // 备注
    onHouseText(row) {
      let that = this;
      this.$prompt("备注", "请输入备注", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputValidator: (value) => {
          if (value) return true;
          return false;
        },
        inputPlaceholder: "请输入备注",
        inputErrorMessage: "请输入备注",
      })
        .then(async ({ value }) => {
          let obj = {
            house_text: value,
            package_number: row.package_number,
            type_no: row.type_no,
          };
          await postAdminOperateSetHumanplatform(obj).then((res) => {
            this.$message({
              type: "success",
              message: "成功",
            });
          });
          let form = that.form;
          this.postAdminOperateGetProducts_house(form);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    // 删除图片
    async onImageDelete(row, index, group_by) {
      let obj;
      if (group_by == 1) {
        let rttimg = row.rttimg;
        rttimg.splice(index, 1);
        let img = rttimg;
        obj = {
          package_number: row.package_number,
          group_by,
          img,
        };
      } else {
        let protimg = row.protimg;
        protimg.splice(index, 1);
        let img = protimg;
        obj = {
          package_number: row.package_number,
          group_by,
          type_no: row.type_no,
          img,
        };
      }
      await postAdminOperateSetHumanplatform(obj);
      let form = this.form;
      this.postAdminOperateGetProducts_house(form);
    },
    // 预览图片
    onImagePreview(e) {
      this.dialogVisible = true;
      this.dialogImageUrl = e;
    },
    async onUpload(row, type) {
      this.row = row;
      this.group_by = type;
      await this.$refs["uploadmy"].$refs["upload-inner"].handleClick();
    },
    // 上传图片
    async beforeAvatarUpload(file) {
      let { row, group_by } = this;
      console.log(file, row, group_by);
      this.loading = true;
      try {
        let data = await uploadHomeApiupload(file);

        let obj;
        if (group_by == 1) {
          let rttimg = row.rttimg || [];
          let img = data.img;
          rttimg.push(img);
          img = rttimg;
          obj = {
            package_number: row.package_number,
            group_by,
            img,
          };
        } else {
          let protimg = row.protimg || [];
          let img = data.img;
          protimg.push(img);
          img = protimg;
          obj = {
            package_number: row.package_number,
            group_by,
            type_no: row.type_no,
            img,
          };
        }
        await postAdminOperateSetHumanplatform(obj);
        let form = this.form;
        this.postAdminOperateGetProducts_house(form);
        this.loading = false;
        return false;
      } catch (error) {
        this.$message.warning(error);
        this.loading = false;
        return false;
      }
    },
    // 采购列表
    async postAdminOperateGetProducts_house(data) {
      this.loading = true;
      this.form = data;
      postAdminOperateGetProducts_house(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminOperateGetProducts_house(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader-iconmy {
  border: 1px dashed #d9d9d9 !important;
  font-size: 20px;
  color: #8c939d;
  width: 78px;
  height: 78px;
  line-height: 78px !important;
  text-align: center;
}
.filemy {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  width: 260px;
  .filemy-img {
    position: relative;
    .filemy-img-image {
      width: 80px;
      height: 80px;
    }
    .filemy-img-popup {
      width: 80px;
      height: 80px;
      position: absolute;
      top: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      background: rgba($color: #000000, $alpha: 0.3);
      color: #ffffff;
      font-size: 20px;
      > i {
        margin: 5px;
      }
    }
  }
}
.dialogcontent {
  > div {
    display: flex;
    margin-bottom: 20px;
    > div {
      margin-right: 30px;
    }
  }
}
.uploaderimage {
  display: flex;
  flex-wrap: wrap;
  .uploaderimage-div {
    position: relative;
    width: 180px;
    height: 180px;
    margin-right: 8px;
    border-radius: 4px;
    overflow: hidden;
    .uploaderimage-div-del {
      background: rgba($color: #000000, $alpha: 0.3);
      position: absolute;
      top: 0;
      left: 0;
      width: 180px;
      height: 180px;
      z-index: 10;
      display: flex;
      align-items: center;
      justify-content: center;
      > i {
        font-size: 28px;
        color: #ffffff;
      }
    }
    .uploaderimage-div-image {
      width: 180px;
      height: 180px;
    }
  }
}
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;

  .WorkingSchedule {
    background: #ffff;
    padding: 20px;
    border-radius: 2px;
    @include boxShadow;
  }
}
</style>
